import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

import 'FavListTitle.dart';
import 'MoreCtrl.dart';
import 'color/MyColor.dart';
import 'model/Classify.dart';
import 'color/MyNetTool.dart';
class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
      ),
      home:MyHomeCtrl(title: "首页",),
    );
  }
}
class MyHomeCtrl extends StatefulWidget{
  MyHomeCtrl({Key key, this.title,this.male}) : super(key: key);

  final String title;
  final String male;
  @override
  State<StatefulWidget> createState() {
    return _MyHomeState();
  }
}
class _MyHomeState extends State<MyHomeCtrl> with AutomaticKeepAliveClientMixin {
  var listData = <Male>[];
  var page     = 0;
  @override
  bool get wantKeepAlive => true;
  @override
  void initState(){
    super.initState();
    this.loadData();
  }
  Future <void>onRefresh() async {
    await Future.delayed(Duration(seconds: 2), (){
      page = 0;
      listData.clear();
      loadData();
    });
  }
  Future<void> loadData() async {
    MyNetTool tool = MyNetTool();
    Map<String ,dynamic> map = await MyNetTool.method(HttpMethod.Get, AppBaseUrl1, "/cats/lv2/statistics", null, null);
    this.listData.clear();
    Classify classif = Classify.fromJson(map);
    print(widget.male);
    if (widget.male.contains("female") == true){
      print("add object data female");
      this.listData.addAll(classif.female);
    }else if(widget.male.contains("male") == true){
      print("add object data male");
      this.listData.addAll(classif.male);
    }else{
      print("add object data other");
      this.listData.addAll(classif.picture);
    }
    setState(() {
    });
  }
  @override
  Widget build(BuildContext context) {
    final height = AppHeigh - AppToolBar - AppStatusBar - AppNaviBar;
    return Scaffold(

      body: Center(

        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new Container(
              height: (height),
              width:  AppWidth,
              color: MyColor.AppFFFFFF,
              child: new RefreshIndicator(child: GridView.builder(
                padding: EdgeInsets.all(15.0),
                itemCount: this.listData.length,
                itemBuilder: (context,index){
                  Male model = this.listData[index];
                  var count = model.monthlyCount;
                  var url = model.bookCover.first;
                  var subTitle =  "月票 : $count";
                  return HomeListTitle(title: model.name,subTitle: subTitle,url: MyUrl.getNewUrl(url),onTap: (){
                    Navigator.push(
                      context,
                      new MaterialPageRoute(
                          builder: (context) => new MoreCtrl(title:model.name,major:model.name,gender: widget.male,)));
                });
                },
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisSpacing: 15,
                  mainAxisSpacing: 15,
                  crossAxisCount: 3,
                  childAspectRatio: 0.5,

                ),
              ), onRefresh: onRefresh),

            ),
          ],
        ),
      ),
    );
  }
}